package com.zlfund.cat.zjgsmk.ui.home.smk_square;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.adapter.AdapterFactory;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewAdapter;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewHolder;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewOneTypeAdapterHelper;
import com.zlfund.cat.zjgsmk.bean.HomeBean;
import com.zlfund.cat.zjgsmk.constant.Constants;
import com.zlfund.cat.zjgsmk.ui.ActivitySwitch;
import com.zlfund.cat.zjgsmk.ui.base.ZApplication;
import com.zlfund.cat.zjgsmk.util.GlideUtil;
import com.zlfund.cat.zjgsmk.util.StatusBarUtils;
import com.zlfund.cat.zjgsmk.util.ViewpagerHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainSquareFragment extends Fragment {

    private final static int FOCUSTYPE = 0;
    private final static int SUGGESTTYPE = 1;

    @BindView(R.id.vp_banner)
    ViewPager vpBanner;
    @BindView(R.id.vp_left)
    View vpLeft;
    @BindView(R.id.vp_right)
    View vpRight;
    @BindView(R.id.iv_hot_serve1)
    ImageView ivHotServe1;
    @BindView(R.id.tv_hot_serve1)
    TextView tvHotServe1;
    @BindView(R.id.hot_serve1)
    RelativeLayout hotServe1;
    @BindView(R.id.iv_hot_serve2)
    ImageView ivHotServe2;
    @BindView(R.id.tv_hot_serve2)
    TextView tvHotServe2;
    @BindView(R.id.hot_serve2)
    RelativeLayout hotServe2;
    @BindView(R.id.iv_hot_serve3)
    ImageView ivHotServe3;
    @BindView(R.id.tv_hot_serve3)
    TextView tvHotServe3;
    @BindView(R.id.hot_serve3)
    RelativeLayout hotServe3;
    @BindView(R.id.iv_hot_serve4)
    ImageView ivHotServe4;
    @BindView(R.id.tv_hot_serve4)
    TextView tvHotServe4;
    @BindView(R.id.hot_serve4)
    RelativeLayout hotServe4;
    @BindView(R.id.iv_hot_serve5)
    ImageView ivHotServe5;
    @BindView(R.id.tv_hot_serve5)
    TextView tvHotServe5;
    @BindView(R.id.hot_serve5)
    RelativeLayout hotServe5;
    @BindView(R.id.iv_hot_serve6)
    ImageView ivHotServe6;
    @BindView(R.id.tv_hot_serve6)
    TextView tvHotServe6;
    @BindView(R.id.hot_serve6)
    RelativeLayout hotServe6;
    @BindView(R.id.iv_hot_serve7)
    ImageView ivHotServe7;
    @BindView(R.id.tv_hot_serve7)
    TextView tvHotServe7;
    @BindView(R.id.hot_serve7)
    RelativeLayout hotServe7;
    @BindView(R.id.iv_hot_serve8)
    ImageView ivHotServe8;
    @BindView(R.id.tv_hot_serve8)
    TextView tvHotServe8;
    @BindView(R.id.hot_serve8)
    RelativeLayout hotServe8;
    @BindView(R.id.iv_hot_serve9)
    ImageView ivHotServe9;
    @BindView(R.id.tv_hot_serve9)
    TextView tvHotServe9;
    @BindView(R.id.hot_serve9)
    RelativeLayout hotServe9;
    Unbinder unbinder;
    @BindView(R.id.divider_vertical)
    View mDividerVertical;
    @BindView(R.id.tv_hot)
    TextView mTvHot;
    @BindView(R.id.tv_new)
    TextView mTvNew;
    @BindView(R.id.rv_focus)
    RecyclerView mRvFocus;
    @BindView(R.id.ll_focus_topic)
    LinearLayout mLlFocusTopic;
    @BindView(R.id.rv_suggest)
    RecyclerView mRvSuggest;
    private ViewpagerHelper mHelper;
    private List<HomeBean.TopimgBean> mBannerBeans;
    private Context mContext;
    private List<HomeBean.SuggestBean> mSuggestBeans;
    private RecyclerViewAdapter<HomeBean.SuggestBean> mSuggestAdapter;
    private RecyclerViewAdapter<HomeBean.NewBean> mFocusAdapter;
    private List<HomeBean.NewBean> mFocusBeans;

    public MainSquareFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            mHelper.stopAutoRoll();
        } else {
            if (mBannerBeans != null && !mBannerBeans.isEmpty()) {
                mHelper.startAutoRoll();
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        mHelper.startAutoRoll();
        ZApplication.mContext.getLoginService().showSquareUI(mLlFocusTopic);
    }

    @Override
    public void onStop() {
        super.onStop();
        mHelper.stopAutoRoll();
    }

    private List<String> mImgUrls;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main_dynamic, container, false);
        unbinder = ButterKnife.bind(this, view);
        mHelper = new ViewpagerHelper(vpBanner);
        mSuggestAdapter = AdapterFactory.singleTon().createOneTypeRvAdapter(mContext, new RecyclerViewOneTypeAdapterHelper<HomeBean.SuggestBean>(R.layout.layout_square_rv_item) {
            @Override
            public void initViews(RecyclerViewHolder holder, final int position, HomeBean.SuggestBean suggestBean) {
                GlideUtil.loadRes(mContext, Constants.ICON_ITEMS1.get(position), holder.getView(R.id.iv, ImageView.class));
                holder.getView(R.id.tv_label,TextView.class).setText(mSuggestBeans.get(position).getLabel());
                setItemClickListener(position, holder, SUGGESTTYPE);
            }
        });
        mFocusAdapter = AdapterFactory.singleTon().createOneTypeRvAdapter(mContext, new RecyclerViewOneTypeAdapterHelper<HomeBean.NewBean>(R.layout.layout_square_rv_item) {
            @Override
            public void initViews(RecyclerViewHolder holder, final int position, HomeBean.NewBean focusBean) {
                GlideUtil.loadRes(mContext, Constants.ICON_ITEMS2.get(position), holder.getView(R.id.iv, ImageView.class));
                holder.getView(R.id.tv_label,TextView.class).setText(mFocusBeans.get(position).getLabel());
                setItemClickListener(position, holder, FOCUSTYPE);
            }
        });
        mRvFocus.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mRvFocus.setAdapter(mFocusAdapter);
        mFocusAdapter.setList(mFocusBeans);
        mRvSuggest.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mRvSuggest.setAdapter(mSuggestAdapter);
        mSuggestAdapter.setList(mSuggestBeans);
        mHelper.addOnLeftAndRightTouchListener(vpLeft, vpLeft);
        if (mImgUrls != null) {
            mHelper.setList(mContext, mImgUrls);
            mHelper.startAutoRoll();
        }
        StatusBarUtils.StatusBarLightMode(getActivity());
        return view;
    }

    public void setItemClickListener(final int position, RecyclerViewHolder holder, final int type){
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == Constants.ICON_ITEMS1.size() - 1){
                    if (type == FOCUSTYPE){

                    }else {

                    }
                }else {
                    if (type == FOCUSTYPE) {
                        ActivitySwitch.toSocialActivity(mContext, mFocusBeans.get(position).getLabel());
                    }else {
                        ActivitySwitch.toSocialActivity(mContext, mSuggestBeans.get(position).getLabel());
                    }
                }
            }
        });
    }

    public void setChannelBeans(List<HomeBean.SuggestBean> list, List<HomeBean.NewBean> list1) {
        mSuggestBeans = list;
        mFocusBeans = list1;
        if (mSuggestBeans != null && mSuggestBeans.size() > 8) {
            mSuggestBeans = mSuggestBeans.subList(0, 8);
        }
        if (mFocusBeans != null && mFocusBeans.size() > 8) {
            mFocusBeans = mFocusBeans.subList(0, 8);
        }
        if (mSuggestAdapter != null) {
            mSuggestAdapter.setList(mSuggestBeans);
            mFocusAdapter.setList(mFocusBeans);
        }
    }

    public void setList(List<HomeBean.TopimgBean> list) {
        if (list == null) return;
        mBannerBeans = list;
        if (mImgUrls == null) mImgUrls = new ArrayList<>();
        mImgUrls.clear();
        for (HomeBean.TopimgBean bean : mBannerBeans) {
            mImgUrls.add(bean.getImg());
        }
        if (mHelper != null) {

            mHelper.setList(mContext, mImgUrls);
            mHelper.startAutoRoll();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
